package com.zzz.demo;

import java.util.Arrays;

public class Demo {

    public static void main(String[] args) {
        int[] arr = new int[]{3, 2, 1, 1};
        new Demo().code(arr);
        System.out.println(Arrays.toString(arr));
    }


    public void code(int[] arr) {
        int low = 0;
        int curr = 0;
        int height = arr.length - 1;

        while (curr <= height) {
            if (arr[curr] == 1) {
                swap(arr, curr, low);
                low++;
                curr++;
            } else if (arr[curr] == 3) {
                swap(arr, curr, height);
                height--;
            } else {
                curr++;
            }
        }
    }

    public void swap(int[] arr, int i, int j) {
        if (i == j) return;
        arr[i] = arr[i] ^ arr[j];
        arr[j] = arr[i] ^ arr[j];
        arr[i] = arr[i] ^ arr[j];
    }

}
